package amap.com.android_path_record;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import amap.com.StepCount.StepForCounter;
import amap.com.fragment.HomeFragment;
import amap.com.fragment.RecordFragment;
import amap.com.fragment.SettingFragment;

import android.hardware.SensorEvent;
import android.hardware.SensorManager;
import android.widget.EditText;
import android.widget.TextView;


public class HomeActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {



    private TextView tvSensors;
    private StepForCounter mStepForCounter;
    private SensorManager sensorManager;

    private View view;
    private EditText edt1,edt2,edt3,edt4;

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment fragment = null;
        switch (item.getItemId()) {
            case R.id.navigation_home:
                fragment = new HomeFragment();
                break;

            case R.id.navigation_record:
                fragment = new RecordFragment();
                break;

            case R.id.navigation_setting:
                fragment = new SettingFragment();
                break;

        }
        return loadFragment(fragment);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);


        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(this);
        loadFragment(new HomeFragment());

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        tvSensors = (TextView) findViewById(R.id.steps);
        mStepForCounter = new StepForCounter(sensorManager);

        userCounterListener();

    }


    private boolean loadFragment(Fragment fragment) {
        if (fragment != null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.home_fragment, fragment).commit();
            return true;
        }
        return false;
    }

    public void userCounterListener() {
        mStepForCounter.setStepListener(new StepForCounter.StepCountListener2() {
            @Override
            public void onStep(SensorEvent event) {
                tvSensors.setText(Math.round(1641) + "步");
            }
        });
    }
}





